<div id="dg_role">
	<input type="hidden" id="user_ids"/>
</div>

<script>
	var form_url = "${ctx}/manager/base/role/add", id = "${id!}" ;
	var $dg_role ;
	$(function(){
	
		$dg_role = $("#dg_role").datagrid({
			url: "${ctx}/manager/base/role/datagrid?companyId=${companyId}",
            method: "GET", idField: "id", fit: true, 
            border: false, striped:true, pageSize: 30, pageList: [30,40,50,100,200,500,1000], 
            pagination: true, rownumbers: true, remoteSort: true, 
            sortName: 'sorting', sortOrder: 'asc', queryParams: {},
            frozenColumns: [[
                { field: "ck", checkbox: true },
                { field: "id", title: "ID", width: 80, sortable: true, hidden: true }
            ]],
            columns: [[
                { field: "fullName", title: "名称", width: 100, formatter:function(value,row){
                	return "<span style='line-height:30px;'>"+value+"</span>" ;
                }},
                { field: "code", title: "角色编码", width: 100, align: "center" }
            ]],
            onLoadSuccess: function(){
            	$.fn.datagrid.extensions.onLoadSuccess.apply(this, arguments);
            	$dg_role.datagrid('unselectAll');$dg_role.datagrid('clearSelections');
            },
            enableHeaderClickMenu: true, enableHeaderContextMenu: true, enableRowContextMenu: false
        });
		
		/*
		if(undefined != id && "" != id) {
			form_url = "${ctx}/manager/base/user/update" ;
			$.ajax({
				type: "GET", dataType: "JSON",
				url: "${ctx}/manager/base/user/get/"+id,
				success: function(result) {
					$("input[name=locked][value="+result.locked+"]").iCheck('check') ;
					
					if(undefined != result.company_id) {
						$company.combotree("setValue", result.company_id);
						$dept.combotree("setValue", result.dept_id);
					}
					
					$("#form").form("load", result); 
					
					$("input[name=password]").attr("type", "text").val("密码不可编辑").prop("disabled", "true") ;
				},
				complete: function(XMLHttpRequest, textStatus){},
				error: function(XMLHttpRequest, textStatus, errorThrown){
					$.easyui.loaded();
					if(XMLHttpRequest.status == 403) {
						$.messager.alert(XMLHttpRequest.statusText, XMLHttpRequest.responseText, "warning");
					} else {
						$.messager.alert("错误", "发生未知错误", "error");
					}
				}
			});
		}
		
		*/
	});
	
	//提交表单数据
	var submitNow = function($d, $dg, flag) {
		var user_ids = $("#user_ids").val() ;
		var rows = $dg_role.datagrid('getChecked');
		var role_ids = [];
		if (rows.length > 0) {
			for ( var i = 0; i < rows.length; i++) {
				if(undefined != rows[i].id && ""!=rows[i].id) {
					role_ids.push(rows[i].id);
				}
			}
		}
		var data = {"user_ids": user_ids, "role_ids": role_ids.join(",")} ;
		
		$.post("${ctx}/manager/base/user/addRoleToUser", data, function(result) {
			if (result.status) {
				$dg.datagrid('reload');alertify.success(result.message);$.easyui.loaded();
				if(flag==undefined||flag==false){$d.dialog("close") ;}
			} else {
				$.easyui.loaded();$.messager.alert("错误", result.message, "error");
			}
		}, 'json').error(function() { $.easyui.loaded(); });
	};
	
	//验证表单
	var submitForm = function($d, $dg, flag) { 
		if($('#form').form('validate')) {
			$.easyui.loading({ msg: "数据提交中，请稍等..." });
			submitNow($d, $dg, flag) ;
		}
	};
</script>

